﻿@model SolrResponse<Resume>

@section header{
    <form class="navbar-form navbar-left" role="search">
        <div class="form-group">
            @Html.TextBox("q", null, new { @class = "form-control", placeholder = "简历中的关键字" })
        </div>
        <button type="submit" class="btn btn-default">搜索</button>
    </form>
}

<div class="filters">
    @RenderFilterLine("Position", "应聘职位")
    @RenderFilterLine("BirthYear", "生于")
    @RenderFilterLine("YearsOfWorking", "工作经验", "年")
    @RenderFilterLine("Gender", "性别")
    @RenderFilterLine("Marriage", "婚姻状况")
    @RenderFilterLine("HuKou", "户籍")
    @RenderFilterLine("Education", "学历")
    @RenderFilterLine("Salary", "期望月薪")
</div>

<div class="row">
    <div class="col-md-4">
        <div class="panel panel-default">
            <div class="panel-heading">共@(Model.Docs.NumFound)份简历</div>
            <div class="list-group">
                @foreach (var resume in Model.Docs.Docs) {
                    <a href="javascript:viewResume('@resume.PhoneNumber')" class="list-group-item">
                        <p class="text-primary">
                            @(resume.YearsOfWorking)年 @resume.Education @resume.GetShortSalary()
                        </p>
                        <p class="list-group-item-text">@resume.GetRecentWorkDuration() @(resume.GetRecentWorkPosition()) @resume.GetRecentWorkCompanyShortName()</p>
                    </a>
                }
            </div>
        </div>
    </div>
    <div class="col-md-8" id="resumeDetails">
    </div>
</div>


@helper RenderFilterLine(string facetKey, string displayName, string suffix = null) {
    var facet = Model.Facet.FacetFields[facetKey];
    var value = Request[facetKey];
    if (facet.Count > 0) {
        <div class="btn-group" data-filter-key="@facetKey" data-filter-value="@value">
            <button type="button" class="btn btn-default dropdown-toggle @{if (value != null) { <text>btn-primary</text> } }" data-toggle="dropdown">
                @displayName @if(value != null){@("：" + value + suffix)} <span class="caret"></span>
            </button>
            <ul class="dropdown-menu" role="menu">
                <li><a href="#" data-filter-value="">全部</a></li>
                <li class="divider"></li>
                @foreach (var option in facet) {
                    var isActive = option.Key == value;
                    <li class="@(isActive ? "active" : string.Empty)">
                        <a href="#" data-filter-value="@option.Key">
                            @(option.Key + suffix)
                            <small>(@option.Value)</small>
                        </a>
                    </li>
                }
            </ul>
        </div>
    }
}

<script type="text/javascript">
    function viewResume(phoneNumber) {
        $('#resumeDetails').load('/Resumes/Details', { phoneNumber: phoneNumber, q: '@Html.Raw(HttpUtility.JavaScriptStringEncode(Request["q"]))' });
    }

    function filter() {
        var params = {};

        var q = $('#q').val();
        if (q)
            params['q'] = q;

        $.each($('.filters').find('div.btn-group'), function (index, item) {
            var $item = $(item);
            var key = $item.attr('data-filter-key');
            var val = $item.attr('data-filter-value');
            if (val)
                params[key] = val;
        });

        window.location = "/Resumes/Index?" + $.param(params);
    }

    $(document).ready(function () {
        var $q = $('#q');

        // 搜索按钮
        $q.closest('form').submit(function (e) {
            e.preventDefault();
            window.location = "/Resumes/Index?q=" + $q.val();
        });

        // 筛选条件
        $.each($('.filters').find('a'), function (index, item) {
            $(item).click(function () {
                var val = $(this).attr('data-filter-value');
                $(this).closest('div.btn-group').attr('data-filter-value', val);
                filter();
            });
        });
    });
</script>
